﻿using Crawler.DBEntities;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Crawler.DataHelper
{
    public static class PostHelper
    {
        /// <summary>
        /// 获取帖子的标题，还需要改进1.有一个网站不是匹配的第一个；2.去掉尾巴
        /// </summary>
        /// <param name="strResponse"></param>
        /// <returns></returns>
        public static string GetPostTitle(string strResponse)
        {
            MatchCollection matches = new Regex(Constant.regexPostTitle).Matches(strResponse);
            Match match = matches[0];
            string title = match.ToString();
            return title;

        }
        /// <summary>
        /// 获取帖子的时间
        /// </summary>
        /// <param name="strSponse"></param>
        /// <returns></returns>
        public static DateTime GetPostTime(string strResponse)
        {
            DateTime dt1 = new DateTime();
            DateTime dt2 = new DateTime();
            MatchCollection matches = new Regex(Constant.regexPostTime).Matches(strResponse);
            //取出两个时间
            string date1 = matches[0].ToString();
            string date2 = matches[1].ToString();
            //分离数字
            string[] s1 = date1.Split('-',':','/',' '); 
            string[] s2 = date2.Split('-', ':', '/',' ');
            //转化成int
            List<int> s1ToInt = new List<int>();
            List<int> s2ToInt = new List<int>();
            foreach (var s in s1)
            {
                int time = int.Parse(s);
                s1ToInt.Add(time);
            }
            foreach (var s in s2)
            {
                int time = int.Parse(s);
                s2ToInt.Add(time);
            }
            //给datetime赋值
            if (s1ToInt.Count == 6)
                dt1 = new DateTime(s1ToInt[0], s1ToInt[1], s1ToInt[2], s1ToInt[3], s1ToInt[4], s1ToInt[5]);
            if (s2ToInt.Count == 6)
                dt2 = new DateTime(s2ToInt[0], s2ToInt[1], s2ToInt[2], s2ToInt[3], s2ToInt[4], s2ToInt[5]);
            //比较
            var timestamp = dt1 - dt2;
            if (timestamp.CompareTo(new TimeSpan(0, 0, 0, 0, 0)) > 0)
                return dt1;
            else
                return dt2;
        }
        /// <summary>
        /// 获取帖子的内容
        /// </summary>
        /// <param name="strResponse"></param>
        /// <returns></returns>
        public static string GetPostContent(string strResponse)
        {
            string contentWithLabel = null;
            string contentWithoutLabel =null;
            MatchCollection matches = new Regex(Constant.regexPostContent).Matches(strResponse);
            if (matches.Count != 0)
            {
                contentWithLabel = matches[0].ToString();
                MatchCollection strMatches = new Regex(Constant.regexPostContent).Matches(contentWithLabel);
                foreach (var match in strMatches)
                {
                    contentWithoutLabel += match.ToString();
                }
            }
            return contentWithoutLabel;
        }
        /// <summary>
        /// 向表中插入帖子的记录
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        public static bool InsertPostToDB(ref Post post)
        {
            bool isInsertSucceed = true;
            MysqlHelper helper = new MysqlHelper();
            if(post.Forum_post_id!=0 && post.Seed_post_id!=0)
                isInsertSucceed = helper.InsertPost(post);
            return isInsertSucceed;
        }
        
    }
}
